﻿using MediatR;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PublicCommon;
using T_settlement.Domain;
using T_settlement.Infrastructure.Interfacec;
using T_settlement.Read.API.Application.Command.TollCommand;

namespace T_settlement.Read.API.Application.CommandHandlers.TollCommandHandlers
{
    public class GetTollCommandHandlers : IRequestHandler<GetTollCommand, PageResult<Settlement>>
    {
        private readonly lsettlementRepository lsettlementRepository;

        public GetTollCommandHandlers(lsettlementRepository lsettlementRepository)
        {
            this.lsettlementRepository = lsettlementRepository;
        }



        public async Task<PageResult<Settlement>> Handle(GetTollCommand request, CancellationToken cancellationToken)
        {
            var list = lsettlementRepository.GetAllAsync().Result.Where(x => x.IsDel == false).AsQueryable();
         
            if (!string.IsNullOrEmpty(request.Nnit))
            {
                list = list.Where(x => x.Nnit.Contains(request.Nnit));
            }

            if (request.PayType != 0&&request.PayType!=null)
            {
                list = list.Where(x => x.PayType == request.PayType);
            }

            if (request.CheckState != 0)
            {
                list = list.Where(x => x.CheckState == request.CheckState);
            }

            if (!string.IsNullOrEmpty(request.Attn))
            {
                list = list.Where(x => x.Attn.Contains(request.Attn));
            }

            if (!string.IsNullOrEmpty(Convert.ToString(request.BusinessTime)))
            {
                list = list.Where(x => x.BusinessTime == request.BusinessTime);
            }
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling((totalCount * 1.0 )/ request.pageSize);
            var res = list.OrderByDescending(x=>x.SettlementId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return new PageResult<Settlement>
            {
                PageCount = pageCount,
                TotalCount = totalCount,
                List = res
            };
        }

    }

}


